package com.yundasys.lab08jvm.threadpool;

import java.util.concurrent.*;

/**
 * @PACKAGE_NAME: com.yundasys.lab08jvm.threadpool
 * @NAME: ThreadPoolTest
 * @USER: jiangta
 * @DATE: 2021/2/12
 * @TIME: 6:16 下午
 * @PROJECT_NAME: springboot-labs
 */
public class ThreadPoolTest {

    public static void main(String[] args) {
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
//                10,
//                10,
//                10L,
//                //默认情况下指的是非核心线程的空闲时间
//                //如果allowCoreThreadTimeout = true： 核心线程/非核心线程允许的空闲时间
//                TimeUnit.SECONDS,
//                new LinkedBlockingQueue<>(),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy()
//        );
//        threadPoolExecutor.allowCoreThreadTimeOut(true);
//
//        threadPoolExecutor.execute(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("线程池测试");
//            }
//        });
//
//        threadPoolExecutor.execute(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("线程池测试2");
//            }
//        });
//
//        Future<String> future = threadPoolExecutor.submit(new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                return "测试submit";
//            }
//        });
//
//        try {
//            String res = future.get();
//        } catch (Exception e) {
//
//        }
        // 手写线程池
        final Integer corePoolSize = 2;
        final Integer maximumPoolSize =5;
        final Long keepAliveTime = 1L;

        ExecutorService executorService = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());

        try {
            for (int i = 0; i < 100; i++) {
                final int tempInt = i;
                executorService.execute(() -> {

                    System.out.println(Thread.currentThread().getName() + "\t 给用户:" + tempInt + " 办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }
}
